﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CruxCRUDBuilderHelper;
using CruxCRUDBuilderHelper.Constants;
using CruxCRUDBuilderHelper.DTOs;
using CruxCRUDBuilderHelper.Enum;
using CruxCRUDBuilderHelper.Utility;

namespace CruxCRUDBuilder
{
    public partial class CodeGenerator : Form
    {
        public CodeGenerator()
        {
            InitializeComponent();
        }

        private void btnProject_Click(object sender, EventArgs e)
        {
            lblResult.Text = string.Empty;
            FolderBrowserDialog ofd = new FolderBrowserDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtProject.Text = ofd.SelectedPath;
                txtDBML.Enabled = true;
                btnBrowse.Enabled = true;
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            lblResult.Text = string.Empty;
            GenerateCode code = new GenerateCode();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "DBML DLL (*.dll)|";
            ofd.Title = "Select Business Object Assembly";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtDBML.Text = ofd.FileName;
                code.GetTables(ofd.FileName);
                btnGenerateTable.Enabled = true;
            }
        }

        private void btnGenerateTable_Click(object sender, EventArgs e)
        {
            lblResult.Text = string.Empty;
            lstTables.Enabled = true;
            lstTables.Items.Clear();
            DataTable tables = TableAttributes.GetDBTables(txtServer.Text, txtUserName.Text, txtPasswd.Text);
            foreach (DataRow row in tables.Rows)
            {
                if (AssemblyInfo.TableNames.ContainsKey(row[2].ToString()))
                {
                    string value = AssemblyInfo.TableNames.Where(a => a.Key == row[2].ToString()).FirstOrDefault().Value;
                    lstTables.Items.Add(new TableInfo { DbTableName = row[2].ToString(), DbmlTableName = value });

                }
            }
            
            lstTables.DisplayMember = "DbmlTableName";
            lstTables.ValueMember = "DbTableName";
        }

        private void btnCode_Click(object sender, EventArgs e)
        {
            try
            {
                lblResult.Text = "Generating Code...";
                GenerateCode code = new GenerateCode();
                List<TableInfo> tables = GetSelectedTables();
                code.GetSelectedTable(tables, txtViewNamespace.Text, txtQueryName.Text, txtProject.Text, txtNamespaceQuery.Text, txtNamespaceFacade.Text, txtNamespaceDTO.Text, txtNamespaceConverter.Text, txtNamespaceController.Text, txtClassConverterName.Text, txtBONamespace.Text, txtSection.Text, txtDataContextVariable.Text);
                List<string> selectedTemplates = GetSelectedTemplates();
                foreach(string template in selectedTemplates)
                {
                    switch (template)
                    {
                        case "DTO":
                            code.GenerateDTO();
                            break;
                        case "View":
                            code.GenerateView();
                            break;
                        case "ClassConverter":
                            code.GenerateClassConverter();
                            break;
                        case "Query":
                            code.GenerateQuery();
                            break;
                        case "Facade":
                            code.GenerateFacade();
                            break;
                        case "Controller":
                            code.GenerateController();
                            break;
                        case "UI":
                            code.GenerateUI();
                            break;
                        default :
                            code.GenerateDTO();
                            code.GenerateView();
                            code.GenerateClassConverter();
                            code.GenerateQuery();
                            code.GenerateFacade();
                            code.GenerateController();
                            code.GenerateUI();
                            break;
                    }
                }
                lblResult.Text = "Code Generated Successfully";
            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message;
            }
        }

        private List<string> GetSelectedTemplates()
        {
            List<string> templates = new List<string>();
            for (int i = 0; i <= lstTemplate.SelectedItems.Count - 1; i++)
            {
                templates.Add(lstTemplate.SelectedItems[i].ToString());
            }
            return templates;
        }

        private List<TableInfo> GetSelectedTables()
        {
            List<TableInfo> tables = new List<TableInfo>();
            for (int i = 0; i <= lstTables.SelectedItems.Count - 1; i++)
            {
                tables.Add((TableInfo)lstTables.SelectedItems[i]);
            }
            return tables;
        }

        private void CodeGenerator_Load(object sender, EventArgs e)
        {
            txtDBML.Enabled = false;
            btnBrowse.Enabled = false;
            btnGenerateTable.Enabled = false;
            lstTables.Enabled = false;
            Template template = Template.DTO;
            lstTemplate.Items.AddRange(Enum.GetNames(template.GetType()));
            List<string> projects = new ProjectSettingParser().GetProjects();
            projects.Insert(0, "select");
            ddlProject.DataSource = projects;
            ddlProject.SelectedIndex = 0;
        }

        private void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ProjectSettingInfo> settings = new ProjectSettingParser().GetProjectSettings(ddlProject.SelectedItem.ToString());
            if (settings.Count > 0)
            {
                foreach (ProjectSettingInfo settingInfo in settings)
                {
                    switch (settingInfo.Name)
                    {
                        case ProjectSettingsConstant.DTONamespace:
                            txtNamespaceDTO.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.ViewNamespace:
                            txtViewNamespace.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.ClassConverterNamespace:
                            txtNamespaceConverter.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.ClassConverterName:
                            txtClassConverterName.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.QueryNamespace:
                            txtNamespaceQuery.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.QueryClassName:
                            txtQueryName.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.FacadeNamespace:
                            txtNamespaceFacade.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.BusinessObjectNamespace:
                            txtBONamespace.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.ControllerNamespace:
                            txtNamespaceController.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.PageSection:
                            txtSection.Text = settingInfo.Value;
                            break;
                        case ProjectSettingsConstant.DataContextVariable:
                            txtDataContextVariable.Text = settingInfo.Value;
                            break;
                        default:
                            ClearAllTextBoxes();
                            break;
                    }
                }
            }
            else 
                ClearAllTextBoxes();
        }

        private void ClearAllTextBoxes()
        {
            txtViewNamespace.Text = string.Empty;
            txtQueryName.Text = string.Empty;
            txtNamespaceQuery.Text = string.Empty;
            txtNamespaceFacade.Text = string.Empty;
            txtNamespaceDTO.Text = string.Empty;
            txtNamespaceConverter.Text = string.Empty;
            txtBONamespace.Text = string.Empty;
            txtClassConverterName.Text = string.Empty;
            txtNamespaceController.Text = string.Empty;
            txtSection.Text = string.Empty;
            txtDataContextVariable.Text = string.Empty;
        }
    }
}
